/*
 * Copyright (C) 2007 Esmertec AG.
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.demonzym.framework.net.httpsocket.xml;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * Represents a XML element of Primitive. Note that this class is not
 * thread-safe.
 */
final public class PrimitiveElement {
    private String mTagName;
    private HashMap<String, String> mAttributes;
    private ArrayList<PrimitiveElement> mChildren;
    private String mContents;

    public PrimitiveElement(String tagName) {
        mTagName = tagName;
    }

    public String getTagName() {
        return mTagName;
    }

    public void setTagName(String tagName) {
        this.mTagName = tagName;
    }

    public Map<String, String> getAttributes() {
        if (mAttributes == null) {
            return null;
        }
        return Collections.unmodifiableMap(mAttributes);
    }

    public void setAttribute(String key, String value) {
        if (key != null && value != null) {
            if (mAttributes == null) {
                mAttributes = new HashMap<String, String>();
            }
            mAttributes.put(key, value);
        }
    }
    public String getAttribute(String key){
    	if (mAttributes == null) {
            return null;
        }
    	return mAttributes.get(key);
    }
    public ArrayList<PrimitiveElement> getChildren() {
        if (mChildren == null) {
            mChildren = new ArrayList<PrimitiveElement>();
        }
        return mChildren;
    }

    public ArrayList<PrimitiveElement> getChildren(String tagName) {
        ArrayList<PrimitiveElement> children = new ArrayList<PrimitiveElement>();

        for (PrimitiveElement child : getChildren()) {
            if (tagName.equals(child.getTagName())) {
                children.add(child);
            }
        }

        return children;
    }

    /**
     * get a direct child which has the input tagName.
     * @param tagName
     * @return a direct child with the input tagName if any, null otherwise.
     */
    public PrimitiveElement getChild(String tagName) {
        for (PrimitiveElement child : getChildren()) {
            if (tagName.equals(child.getTagName())) {
                return child;
            }
        }
        return null;
    }

    public String getChildContents(String tagName) {
        PrimitiveElement child = getChild(tagName);
        return child == null ? null : child.getContents();
    }

    public int getChildCount() {
        if (mChildren == null || mChildren.isEmpty()) {
            return 0;
        } else {
            return mChildren.size();
        }
    }

    public PrimitiveElement getFirstChild() {
        if ((mChildren == null) || mChildren.isEmpty()) {
            return null;
        }
        return mChildren.get(0);
    }

    public PrimitiveElement addChild(PrimitiveElement child) {
        if (child != null) {
            getChildren().add(child);
        }

        return child;
    }

    public PrimitiveElement addChild(String tagName) {
        if (null == tagName) {
            return null;
        }
        PrimitiveElement element = new PrimitiveElement(tagName);
        getChildren().add(element);
        return element;
    }

    public PrimitiveElement addChild(String tagName, String contents) {
        PrimitiveElement element = addChild(tagName);
        if (null != contents) {
            element.setContents(contents);
        }
        return element;
    }

    public void addChild(String tagName, boolean value) {
        addChild(tagName).setContents(value ?
        		"true" : "false");
    }

    public void addPropertyChild(String name, String value)
    {
        PrimitiveElement ret = addChild(ImpsTags.Property);
        ret.addChild(ImpsTags.Name, name);
        ret.addChild(ImpsTags.Value, value);
    }

    public void addPropertyChild(String name, boolean value)
    {
        PrimitiveElement ret = addChild(ImpsTags.Property);
        ret.addChild(ImpsTags.Name, name);
        ret.addChild(ImpsTags.Value, value);
    }

    public String getContents() {
        return mContents;
    }

    public void setContents(String contents) {
        mContents = contents;
    }

	@Override
	public String toString() {
		// TODO Auto-generated method stub
		StringBuffer strPrimitive = new StringBuffer("<");
		strPrimitive.append(getTagName());    
		Map<String, String> attrMap = getAttributes();
		if(attrMap != null && attrMap.size() > 0) {
		    for (Map.Entry<String, String> entry : attrMap.entrySet()) {
		    	strPrimitive.append(' ');
		    	strPrimitive.append(entry.getKey());
		    	//strPrimitive.append("=\"");
		    	strPrimitive.append("=\'");
//		    	String tmp=entry.getValue();
		        writeEncoded(strPrimitive, entry.getValue());
		        //strPrimitive.append('"');
		        strPrimitive.append("\'");
		    }
		}
		
		if (getContents() != null) {
			strPrimitive.append('>');
		    writeEncoded(strPrimitive, getContents());
		
		    strPrimitive.append("</");
		    strPrimitive.append(getTagName());
		    strPrimitive.append('>');
		} else if (getChildCount() > 0) {
			strPrimitive.append('>');
		
		    for (PrimitiveElement child : getChildren()) {
		    	strPrimitive.append(child.toString());
		    }
		    strPrimitive.append("</");
		    strPrimitive.append(getTagName());
		    strPrimitive.append('>');
		} else {
			strPrimitive.append("/>");
		}
		
		/*try {
			return new String(strPrimitive.toString().getBytes("UTF-8"));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}*/
		return strPrimitive.toString();
	}
	private void writeEncoded(StringBuffer strBuf, String str) {
        int len = str.length();
        for (int i = 0; i < len; i++) {
            char ch = str.charAt(i);

            switch (ch) {
                case '<':
                	strBuf.append("&lt;");
                    break;

                case '>':
                	strBuf.append("&gt;");
                    break;

                case '&':
                	strBuf.append("&amp;");
                    break;

                case '"':
                	strBuf.append("&quot;");
                    break;

                case '\'':
                	strBuf.append("&apos;");
                    break;

                default:
                	strBuf.append(ch);
            }
        }
    }
    
}
